# ———————————————————————–
# <copyright file="IIS.ps1" company="The MITRE Corporation">
#    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#    http:#www.apache.org/licenses/LICENSE-2.0
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
# </copyright>
# ———————————————————————–
################################################/
# IIS.ps1 - Creates IIS Directory Structure and configures IIS
# Project: IC.NET Installer
# Author: Don McGarry
################################################/

# Copyright (c) 2013 The MITRE Corporation. All rights reserved.
#
# NOTICE
# This software was produced for the U. S. Government
# under Contract No. FA8721-09-C-0001, and is
# subject to the Rights in Noncommercial Computer Software
# and Noncommercial Computer Software Documentation Clause
# (DFARS) 252.227-7014 (JUN 1995)

$Username=$Args[0];
$Password=$Args[1];
$icnetIISPath =$Args[2];
$IISSite=$Args[3];
$RESTAppPool=$Args[4];
$Port=$Args[5];
$InstallerRoot=$Args[6];


#Register IIS
&c:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i

#Remove IIS Default Stuff for system hardening
if((get-website | where-object { $_.name -eq 'Default Web Site' }) -ne $NULL)
{
	Remove-Website -Name "Default Web Site"
}

if(Test-Path 'IIS:\AppPools\ASP.NET v4.0')
{
	Remove-WebAppPool -Name "ASP.NET v4.0"
}

if(Test-Path 'IIS:\AppPools\ASP.NET v4.0 Classic')
{
	Remove-WebAppPool -Name "ASP.NET v4.0 Classic"
}

if(Test-Path 'IIS:\AppPools\Classic .NET AppPool')
{
	Remove-WebAppPool -Name "Classic .NET AppPool"
}

if(Test-Path 'IIS:\AppPools\DefaultAppPool')
{
	Remove-WebAppPool -Name "DefaultAppPool"
}

if(Test-Path 'IIS:\AppPools\.NET v4.5')
{
	Remove-WebAppPool -Name ".NET v4.5"
}

if(Test-Path 'IIS:\AppPools\.NET v4.5 Classic')
{
	Remove-WebAppPool -Name ".NET v4.5 Classic"
}

#Create the Directory Structure
if(!(Test-Path $icnetIISPath))
{
	md $icnetIISPath
}

if(!(Test-Path $icnetIISPath"bin"))
{
	md $icnetIISPath"bin"
}

if(!(Test-Path $icnetIISPath"Images"))
{
	md $icnetIISPath"Images"
}

if(!(Test-Path $icnetIISPath"lib"))
{
	md $icnetIISPath"lib"
}

if(!(Test-Path $icnetIISPath"Resources"))
{
	md $icnetIISPath"Resources"
}

if(!(Test-Path $icnetIISPath"Scripts"))
{
	md $icnetIISPath"Scripts"
}

if(!(Test-Path $icnetIISPath"Styles"))
{
	md $icnetIISPath"Styles"
}

if(!(Test-Path $icnetIISPath"Styles\images"))
{
	md $icnetIISPath"Styles\images"
}

if(!(Test-Path $icnetIISPath"XSLT"))
{
	md $icnetIISPath"XSLT"
}

#CHECK THIS...
#cp $InstallerRoot\Lib\*.* $icnetIISPath
#cp $InstallerRoot\Lib\*.* $icnetIISSvcPath

#Create IIS App Pools
if(!(Test-Path "IIS:\AppPools\$RESTAppPool"))
{
	New-WebAppPool -Name $RESTAppPool
}

Set-ItemProperty IIS:\AppPools\$RESTAppPool managedRuntimeVersion v4.0
Set-ItemProperty IIS:\apppools\$RESTAppPool -name processModel -value @{userName=$Username;password=$Password;identitytype=3}

#Create Base Router Website
if((get-website | where-object { $_.name -eq $IISSite }) -eq $NULL)
{
	#This is a fix for when there are no websites on the server, need to set the ID manually
	$id = (dir iis:\sites | foreach {$_.id} | sort -Descending | select -first 1) + 1
	New-Website -Name $IISSite -Port $Port -ApplicationPool $RESTAppPool -PhysicalPath $icnetIISPath -id $id
}

#Copy in the svc files and web.config files
cp $InstallerRoot\IIS\crossdomain.xml $icnetIISPath

#Set the application pools to always running for faster response
Set-ItemProperty IIS:\AppPools\$RESTAppPool -Name startMode -Value 1

#Add URL reservation for the core router service
#netsh http add urlacl url=http://+:$Port/RoutingService user="NETWORK SERVICE"
#netsh http add urlacl url=http://+:$SvcPort/RoutingService user="NETWORK SERVICE"